<template>
	<view class="page">

		<view style="margin-left: 20px;margin-right: 20px;">
			<text style="word-break: break-all;">{{ message }}</text>
		</view>
		<scroll-view class="border list" scroll-y="true">
			<view v-for="(item,index) in settingList" :key="index" class="list-item" @click="settingItemClick(index)">
				<text class="text">{{item.text}}</text>
			</view>
		</scroll-view>
	</view>
</template>

<script>
		var systemInfo;
    var ble;
	var _this;
	export default {
		data() {
			return {
				title: 'Setting',
				message: "",
				settingList: [{
					text: "Start ECG"
				}],
				countDown: 60, // 初始倒计时时间，单位秒
				timer: null, // 计时器对象
				countDownText: '倒计时 60 秒' // 显示倒计时的文本
			};
		},

		methods: {
			onLoad() {
				_this = this;
					_this.initMethods();
					var globalEvent = uni.requireNativePlugin('globalEvent');
					globalEvent.addEventListener("ecgEvent", function(res) {
							console.log(JSON.stringify(res.data));
					});
			},
			isAndroidOrIOS() {
				let type = uni.getSystemInfoSync().platform
				switch (type) {
					case 'android':
						systemInfo = "android";
						break;
					case 'ios':
						systemInfo = "ios";
						break;
					default:
						systemInfo = "other";
						break;
				}
			},
			initMethods() {
				
				//判断是安卓和ios系统
				_this.isAndroidOrIOS();
				if (systemInfo == "android") {
					console.log("android bluetooth-ycSdkPlugin");
					ble = uni.requireNativePlugin('bluetooth-ycSdkPlugin');
				} else {
					console.log("ios DCTestUniPlugin");
					ble = uni.requireNativePlugin('DCTestUniPlugin');
				}
				console.log("initPlugin start123" + ble);
				//初始化插件
				ble.initPlugin((res) => {
					console.log("initPlugin" + res.message);
			
				});
				console.log("initPlugin end")
			},
			settingItemClick(index) {
				console.log("点击index:" + index + ",text:" + _this.settingList[index].text);
				uni.showLoading({
					title: '加载中'
				});
				switch (index) {
					case 0:
					//开始ecg测量
						ble.startECGMeasurement((res) => {
							uni.hideLoading();
							console.log("开始测量ECG数据:"+res.data);
							_this.message = res.data;
							//60秒倒计时
							_this.startCountDown();
						});
						break;
					default:
						break;
				}
			},
			startCountDown() {
				_this.timer = setInterval(() => {
					if (_this.countDown > 0) {
						_this.countDown--; // 每秒减少1
						_this.countDownText = `倒计时 ${_this.countDown} 秒`;
							console.log("countDown:"+_this.countDown);
					} else {
						console.log("countDown结束:"+_this.countDown);
						clearInterval(_this.timer); // 倒计时结束，清除计时器
						//倒计时结束，停止测量
						ble.stopECGMeasurement((res) => {
							uni.hideLoading();
							console.log(res.data);
							_this.message = res.data;
							//获取ecg测量结果
							ble.getECGResult(0,(res) =>{
								console.log("ecg结果:"+res.data);	
							});
						});
					}
				}, 1000); // 每秒钟执行一次
			}

		}
	}
</script>

<style lang="scss">
	.page {
		padding-top: 20px;
		padding-bottom: 20px;
	}

	.row {
		display: flex;
		flex-direction: row;
		align-content: flex-start;
		align-items: flex-start;

	}

	.list {
		margin-top: 100px;
		margin-bottom: 100px;

		.list-item {
			background-color: #f8f8f8;

			.text {
				height: 50px;
				line-height: 50px;
				margin-left: 20px;
				color: #003366;
			}
		}
	}

	.log {
		width: 87vw;
		font-size: 13px;
		color: #003366;

		.log-bottom {
			height: 10vw;
		}
	}
</style>